﻿package com.ourbrander.webObj.loader{
 
	
	import flash.display.Loader;
	import flash.display.Sprite;
	 
	import flash.events.Event;
	import flash.events.ProgressEvent
	import flash.events.IOErrorEvent
	import com.ourbrander.Event.superEvent;
	import com.ourbrander.xmlObject.xmlFrame;
	import flash.events.EventDispatcher
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import com.ourbrander.Event.superEvent
	import flash.display.Stage
	import flash.xml.XMLNode
	
	public class preLoader {
		
		public static const  COMPELETE = "compelete"
		public static const  PUASE = "puase"
		public static const  STOP = "stop"
		public static const  USERLOADING = "userLoading"
		public static const  USERCOMPELETE = "userCompelete"
		public static const  SINGLE_LOADED="single_loaded"
		
		private static  var _singleton:Boolean;//是否是单例
		private static var _obj:preLoader//声明的preLoader对象实例
		private static var _userLoading:Boolean//是否用户在主动下载，默认为否
		private static var _canpreLoad:Boolean=true//是否允许预加载，默认为允许
		private var _source:xmlFrame;//预加载的xml化序列对象
		private var _index:uint//下载索引
		private var _load:Loader//下载容器
		private var _count:uint//下载文件总数
		private var _faildIndex:Array//下载失败的文件索引
		//public var _parent:Object//临时用
		public  var _eventDispacher:EventDispatcher //发出和接受各种事件
		
		public function preLoader() {
			
			if (_singleton!=true) {
				init();
			} else {

				throw new Error("本类是单例类，全局类只能有一个");

			}//end if
		}//end function
		private function init() {
			_singleton = true;
			 preLoader.obj = this
			_index = 0
			_faildIndex=new Array()
			_eventDispacher=new EventDispatcher()
			_source = new xmlFrame()
			_load = new Loader()
			
			
			_load.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded)
			_load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loading)
			_load.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError)
			_source.addEventListener(preLoader.USERLOADING, puaseLoad)
			_source.addEventListener(preLoader.USERCOMPELETE, load)
			
		
		}
		//设定预加载的列表XML文件
		public function set source(xml:*) {
			if (xml is XML) {
				_source.addEventListener(Event.COMPLETE,sourceLoaded);
				_source.xml = xml;

			} else {
				
				_source.addEventListener(Event.COMPLETE, sourceLoaded);

				_source.loadXML(xml);
			}
		}
		
		public function get source() {
			
             switch(_source.xmlState){
				case "init":
				//trace("还没有设置预加载的数据源")
				return _source
				//throw new Error("还没有设置预加载的数据源");
				break;
				case "loading":
				//trace("XML数据还未加载完")
				return _source
				break;
				case "compelete":
				return  _source
				break;
			
			 }
			 
		}//end function
		
		
		public  static function set obj(object:preLoader) {
			_obj=object		}
		public  static function get obj() :preLoader{
			return _obj
		}
		public static function set canpreLoad(value:Boolean) {
			preLoader._canpreLoad=value
		}
		public static function get canpreLoad():Boolean {
			return preLoader._canpreLoad
		}
		
		
		private function sourceLoaded(e :Event= null) {
			//当预加载xml数据清单下载完毕
			// trace("当预加载xml数据清单下载完毕")
			 _count = source.xml.child("*").length()
			 // trace("_count:"+_count)
			// startLoad(e)
		}
		
		//开始下载
		public function startLoad(e = null) {
			//trace("startLoad")
		 load(e)
		}
		//暂停下载
		private function puaseLoad(e = null) {
			// _parent._txt.text+=("预加载文件被取消，当前的INDEX：" + index +"\n")
			index=(index>0)?--index:index
		  // _parent._txt.text+=("预加载文件被取消，取消后的INDEX：" + index +"\n")
			try{
		     _load.close()
			 
			//trace(" 取消预加载容器的加载动作")
		
			}catch (e) {
				//trace(e)
				_load.unload()
				//trace("预加载容器内没有在下载")
		}
	
		}
		//停止下载
		private function stopLoad(e=null) {
		
		}
		//获取和设置下载索引
		private function set index(num:uint) {
			_index=num
		}
		private function get index() {
			 
			return _index 
		}
		//下载
		private function load(e = null) {
			//trace(index+"/"+_count)
			   if (!canpreLoad) {
				     trace("不允许预加载")
				    return false
			   }
			   if (index >= _count ) {
				   trace("所有清单文件下载完成")
				  return false
			   }
			   index++
			   var path = source.xml.child(index-1) 
			   _load.load(new URLRequest(path))
			  // trace(source.xml.child(index - 1))
			  
			   
		}
		//清单文件正在下载中
		private function loading(e:ProgressEvent = null) {
			 
		//  trace("预加载文件" + index + "：正在下载>" + e.bytesLoaded + "/" + e.bytesTotal)
		 
		
		}
		//清单某文件下载完毕
		private function loaded(e:Event = null) {
			// trace("文件" + e.target.content + "  :   " + index + "：下载完毕")
			 
			_load.unload()
			if (index <= _count - 1) {
				
				var event = new superEvent(preLoader.SINGLE_LOADED,{index:index, length:_count },true)
				source.dispatchEvent(event)
				if (!preLoader.userLoading) {
					 
					 load()
				}
			   
			}else {
			// trace("下载完毕")
			 
			  event = new superEvent(preLoader.COMPELETE,true)
			 source.dispatchEvent(event)
			}
		}//end function
		private function addfaildIndex(Num:uint) {
			_faildIndex.push(Num)
		}
		private function get faildIndex() {
			return _faildIndex
		}
		private function loadError(e = null) {
			trace("错误的索引" + index+":"+e)
			// _parent._txt.text+=("错误的索引：" + index+"\n--------")
			addfaildIndex(index)//记录下下载失败的文件索引
			load()//跳过错误的下载，继续后面的
		}
		//设置是否用户在主动下载的状态
		public static function set userLoading(boolean:Boolean) {
			preLoader._userLoading=boolean
			var str=(preLoader._userLoading)?preLoader.USERLOADING:preLoader.USERCOMPELETE
		    var event  = new Event(str)
			
		
		    preLoader.obj.source.dispatchEvent(event)
		}
		
		public static function get userLoading() {
			return preLoader._userLoading
		}
		
		
		
		private function check(e) {
		//	trace(e)
		}
		//对需要下载的xml对象进行排序，确定下载顺序。
		
	}
}